package com.disruptor.disruptor;

import com.disruptor.bean.DataBean;
import com.jfinal.plugin.activerecord.Record;
import com.lmax.disruptor.RingBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by mengdexuan on 2017/9/18 10:38.
 *
 * DataBean 生产者
 */
public class DataBeanProducer {

	private static Logger log = LoggerFactory.getLogger(DataBeanProducer.class);

	private final RingBuffer<DataBean> ringBuffer;

	public DataBeanProducer(RingBuffer<DataBean> ringBuffer) {
		this.ringBuffer = ringBuffer;
	}

	public void publishData(Record record) {
		long sequence = ringBuffer.next();  // Grab the next sequence
		DataBean event = null;
		try {
			event = ringBuffer.get(sequence); // Get the entry in the Disruptor
			event.setId(record.getInt("id"));
			event.setName(record.getStr("name"));
			event.setStatus(record.getInt("status"));
		} finally {
			ringBuffer.publish(sequence);
			log.info("生产数据(P) --> "+event);
		}
	}
}
